<?php
class Article{
    protected $main_table = 'data_article';
	protected $main_table2 = 'data_article_cate';	
	protected $main_id = 'article_id';
	protected $main_id2 = 'cate_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	public function get_table_name2(){
	    return $this->main_table2;
	    
	}	
	/**
	* 	insert article
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	insert article_cate
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data_cate($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table2, $data);
		$insert_id = $this->db->getInsertId();
		$this->update_cate_file();
		return $insert_id;	
	}
	
		/**
	* 	update article_cate
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data_cate($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table2,$data,$where);
		$this->update_cate_file();
		return $result;
	}
	
	
	/**
	* 	get article_cate info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_cate_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table2." WHERE ".$this->main_id2."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
		/**
	* 	update article_cate
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_cate_file(){
		$cate_list = $this->get_cate_array('1');
		$temp_ary = array();
		foreach($cate_list as $v){
			$temp_ary[$v['cate_id']] = $v['cate_name'];
		}
		$new_array = "\$article_cate=";
		$new_array .= var_export($temp_ary,true);
		write_file(INFO_PATH.'cf_article_cate.php',$new_array,"php");	
	}
	
	/**
	* 	delete article_cate
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data_cate($id){
		$result = $this->db->delete($this->main_table2,$this->main_id2."='".intval($id)."' ");
		$this->update_cate_file();
		return $result;
	}	
	
	
	/**
	* 	update article
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete article
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($id){
		$result = $this->db->delete($this->main_table,$this->main_id."='".intval($id)."' ");
		return $result;
	}	
	
	/**
	* 	get article info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	public function get_row_by_where($where,$field='*'){		
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where;
		$data = $this->db->getRow($sql);
		return $data;
	}	
	
	/**
	* 	get goods array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='article_id DESC',$limit='10',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}   
	
	/**
	* 	get goods array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_cate_array($where,$order='cate_sort_num ASC',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table2." WHERE ".$where." ORDER BY ".$order;		
		$data = $this->db->getAll($sql);
		return $data;
	} 

	//手机端的文章列表
	public function get_article_list($where,$page,$order="article_id DESC",$field="*",$pageCount=10){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order." LIMIT ".($page-1)*$pageCount.",".$pageCount;
		$list = $this->db->getAll($sql);		
		return $list;
	}
		

}

?>